package cn.keniykey.mc.viewlibrary.view.api;

import java.util.HashMap;

import javax.annotation.Nullable;

public final class FrameViewBundle{
	private HashMap<String,Object> hashmap_bundle = new HashMap<>();
	
	public void putValue(String key,@Nullable Object value) {
		if(null == value) {
			this.hashmap_bundle.remove(key);
			return;
		}
		this.hashmap_bundle.put(key, value);
	}
	public boolean hasValue(String key) {
		return this.hashmap_bundle.containsKey(key);
	}
	@SuppressWarnings("unchecked")
	@Nullable
	public <T> T getValue(String key,@Nullable T defalut_value) {
		return (T) this.hashmap_bundle.getOrDefault(key, defalut_value);
	}
	@SuppressWarnings("unchecked")
	@Nullable
	private <T> T getValue(String key, Class<T> clazz,@Nullable T defalut_value) {
		if(!this.hasValue(key)) return defalut_value;
		final Object obj = this.hashmap_bundle.get(key);
		if(obj.getClass().isInstance(clazz)) return (T) obj;
		return defalut_value;
	}
	public String getString(String key,@Nullable String default_value) {
		return this.getValue(key, String.class, default_value);
	}
	public byte getByte(String key,byte defalut_value) {
		return this.getValue(key, byte.class, defalut_value);
	}
	public char getChar(String key,char defalut_value) {
		return this.getValue(key, char.class, defalut_value);
	}
	public int getInt(String key,int defalut_value) {
		return this.getValue(key, int.class, defalut_value);
	}
	public long getLong(String key,long defalut_value) {
		return this.getValue(key, long.class, defalut_value);
	}
	public float getFloat(String key,float defalut_value) {
		return this.getValue(key, float.class, defalut_value);
	}
	public double getDouble(String key,double defalut_value) {
		return this.getValue(key, double.class, defalut_value);
	}
	@SuppressWarnings("deprecation")
	@Override
	protected void finalize() throws Throwable {
		super.finalize();
		this.hashmap_bundle.clear();
	}
}